<?php 
/**
 * 微信会员控制器
 */
class Member extends Weixin_Controller
{
	
	function __construct()
    {
        parent::__construct();

		$this->load->model('Weixin/Member_model', 'member');
		$this->load->model('Token_model', 'token');
	}

	/**
	 * [获取微信openid]
	 * @Author   zt
	 * @DateTime 2018-08-13
	 * @return   [type]     [description]
	 */
	function getOpenId()
	{
		$post = $this->checkSign();

		$echo = $this->token->getOpenId($post);

		echo json_encode($echo);
	}

	/**
	 * [获取小程序二维码]
	 * @Author   zt
	 * @DateTime 2018-08-13
	 * @return   [type]     [description]
	 */
	function getQrCode()
	{
		$post = $this->checkSign();

		if (isset($post['scene']) && isset($post['page'])) {
			// 生成二维码获取本地文件
			$res = $this->token->getQrCode($post);

			// 将图片上传至阿里云
			$this->load->library('Uploader/Uploader');
			$upload = new Uploader();
			$result = $upload->upload_aliyun($res['name'],$res['tmp_name']);

			if ($result['error'] == 0) {
				$echo = array(
					'code' => 1,
					'url' => $result['url'],
					'msg' => '二维码获取成功',
				);
			} else {
				$echo = array(
					'code' => 0,
					'msg' => '二维码获取失败',
				);
			}

			// 删除本地文件
			unlink($res['tmp_name']);
		} else {
			$echo = array(
				'code' => 0,
				'msg' => '参数丢失',
			);
		}

		echo json_encode($echo);
	}

	/**
	 * [会员添加]
	 * @Author   zt
	 * @DateTime 2018-08-06
	 * @return   [type]     [description]
	 */
	function memberAdd()
	{
		$post = $this->checkSign();

		$echo = $this->member->memberAdd($post);

		echo json_encode($echo);
	}

	/**
	 * [修改会员信息]
	 * @Author   zt
	 * @DateTime 2018-08-06
	 * @return   [type]     [description]
	 */
	function memberUpdate()
	{
		$post = $this->checkSign();

		$echo = $this->member->memberUpdate($post);

		echo json_encode($echo);
	}

	/**
	 * [获取会员详情]
	 * @Author   zt
	 * @DateTime 2018-08-06
	 * @return   [type]     [description]
	 */
	function memberDetail()
	{
		$post = $this->checkSign();

		$echo = $this->member->memberDetail($post);

		echo json_encode($echo);
	}

	/**
	 * [作品发布]
	 * @Author   zt
	 * @DateTime 2018-08-06
	 * @return   [type]     [description]
	 */
	function workAdd()
	{
		$post = $this->checkSign();

		$echo = $this->member->workAdd($post);

		echo json_encode($echo);
	}

	/**
	 * [获取作品列表]
	 * @Author   zt
	 * @DateTime 2018-08-06
	 * @return   [type]     [description]
	 */
	function workList()
	{
		$post = $this->checkSign();

		$echo = $this->member->workList($post);

		echo json_encode($echo);
	}

	/**
	 * [作品删除]
	 * @Author   zt
	 * @DateTime 2018-08-17
	 * @return   [type]     [description]
	 */
	function workDel()
	{
		$post = $this->checkSign();

		$echo = $this->member->workDel($post);

		echo json_encode($echo);
	}

	/**
	 * [档期添加]
	 * @Author   zt
	 * @DateTime 2018-08-06
	 * @return   [type]     [description]
	 */
	function daysAdd()
	{
		$post = $this->checkSign();

		if (isset($post['date']) && !empty($post['date'])) {
			// 获取农历日期
			$this->load->library('Lunar');
			$lunar = new Lunar();
			$lunar_date = $lunar->convertSolarToLunar(date('Y', strtotime($post['date'])), date('m', strtotime($post['date'])), date('d', strtotime($post['date'])));
			$post['lunar_date'] = $lunar_date[1].$lunar_date[2];

			// 获取日期周
			$weekarray = array('日', '一', '二', '三', '四', '五', '六');
			$post['week'] = '周'.$weekarray[date('w', strtotime($post['date']))];

			$echo = $this->member->daysAdd($post);
		} else {
			$echo = array('code' => 0, 'msg' => '日期参数丢失');
		}

		echo json_encode($echo);
	}

	/**
	 * [档期列表]
	 * @Author   zt
	 * @DateTime 2018-08-06
	 * @return   [type]     [description]
	 */
	function daysList()
	{
		$post = $this->checkSign();

		$echo = $this->member->daysList($post);

		echo json_encode($echo);
	}

	/**
	 * [获取档期详情]
	 * @Author   zt
	 * @DateTime 2018-08-07
	 * @return   [type]     [description]
	 */
	function daysDetail()
	{
		$post = $this->checkSign();

		$echo = $this->member->daysDetail($post);

		echo json_encode($echo);
	}

	/**
	 * [档期修改]
	 * @Author   zt
	 * @DateTime 2018-08-06
	 * @return   [type]     [description]
	 */
	function daysUpdate()
	{
		$post = $this->checkSign();

		if (isset($post['date']) && !empty($post['date'])) {
			// 获取农历日期
			$this->load->library('Lunar');
			$lunar = new Lunar();
			$lunar_date = $lunar->convertSolarToLunar(date('Y', strtotime($post['date'])), date('m', strtotime($post['date'])), date('d', strtotime($post['date'])));
			$post['lunar_date'] = $lunar_date[1].$lunar_date[2];

			// 获取日期周
			$weekarray = array('日', '一', '二', '三', '四', '五', '六');
			$post['week'] = '周'.$weekarray[date('w', strtotime($post['date']))];

			$echo = $this->member->daysUpdate($post);
		} else {
			$echo = array('code' => 0, 'msg' => '日期参数丢失');
		}
		

		echo json_encode($echo);
	}

	/**
	 * [知识星球列表]
	 * @Author   zt
	 * @DateTime 2018-08-06
	 * @return   [type]     [description]
	 */
	function loreList($page = 1)
	{
		$post = $this->checkSign();

		$echo = $this->member->loreList($page, $post);

		echo json_encode($echo);
	}

	/**
	 * [获取查档列表]
	 * @Author   zt
	 * @DateTime 2018-08-06
	 * @return   [type]     [description]
	 */
	function checkDays()
	{
		$post = $this->checkSign();

		$echo = $this->member->checkDays($post);

		echo json_encode($echo);
	}
}